﻿using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;

// 系统需在transform系统之后运行 否则会出现镜头抖动
[UpdateInGroup(typeof(LateSimulationSystemGroup))]
partial class CameraSystem : SystemBase
{
    protected override void OnCreate()
    {
        RequireForUpdate<PlayerTag>();
    }

    protected override void OnUpdate()
    {
        Entity target = SystemAPI.GetSingletonEntity<PlayerTag>();
        
        if (CameraSingleton.instance == null) return;
        CameraSingleton.instance.targetTransform = SystemAPI.GetComponent<LocalTransform>(target);

        CameraSingleton.instance.IsFound = true;

        var cameraTransform = CameraSingleton.instance.transform;

        var targetTransform = SystemAPI.GetComponent<LocalTransform>(target);
        cameraTransform.position = new Vector3(targetTransform.Position.x, targetTransform.Position.y, targetTransform.Position.z - 10);
        cameraTransform.LookAt(targetTransform.Position);
    }
}